﻿
// #define ENABLE_ARCHIEVE

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using UDS.Components;
using UDS.Entity;

namespace UDS.SubModule.UnitiveDocument.Mail
{
    /// <summary>
    /// ReadMail 的摘要说明。
    /// </summary>
    public partial class ReadMailPage : Page
    {
        protected int MailID
        {
            get;
            private set;
        }

        protected int CurrentPageIndex
        {
            get;
            private set;
        }

        protected MailFolderType FolderType
        {
            get;
            private set;
        }

        protected int Action
        {
            get;
            private set;
        }

        protected int ClassID
        {
            get;
            private set;
        }

        protected MailFullInfo MailContent
        {
            get;
            private set;
        }

        override protected void OnInit(EventArgs e)
        {
            // Hook event handlers
            btnDelete.Click += btnDelete_Click;

            // Read parameters
            CurrentPageIndex = QSUtil.GetPositiveInt32("CurrentPageIndex");
            MailID = QSUtil.GetPositiveInt32("MailID");
            FolderType = (MailFolderType)QSUtil.GetPositiveInt32("FolderType", (int)MailFolderType.Index);
            Action = QSUtil.GetPositiveInt32("Action");
            ClassID = QSUtil.GetPositiveInt32("ClassID");

            btnReply.RedirectUrl = string.Format("Compose.aspx?Action={0}&MailID={1}", (int)MailAction.Reply, MailID);
            btnForward.RedirectUrl = string.Format("Compose.aspx?Action={0}&MailID={1}", (int)MailAction.Forward, MailID);
            btnBack.RedirectUrl = string.Format("Index.aspx?FolderType={0}&CurrentPageIndex={1}", (int)FolderType, CurrentPageIndex);

#if ENABLE_ARCHIEVE
            if (Action == 3)
            {
                MailIncoming(MailID, ClassID);
            }
#endif

            // Read mail info
            MailContent = UDSMail.GetMailFullInfo(MailID);
            if (MailContent == null)
            {
                SiteContext.Current.Alert("指定邮件不存在。", true);
                Response.End();
            }

            attachmentsRow.Visible = false;
            if (MailContent.Attachments != null && MailContent.Attachments.Count > 0)
            {
                attachmentsRow.Visible = true;
                attachmentRepeater.DataSource = MailContent.Attachments;
                attachmentRepeater.DataBind();
            }

            // TODO:
            // Add attachment support.
            // 开始读取附件信息
            //using (var dataReader = mailclass.GetMailAttInfoDbreader(MailID.ToString()))
            //{
            //    while (dataReader.Read())
            //    {
            //        lblAttachFile.Text += "&nbsp;<a href='Download.aspx?destFileName=" + Server.UrlEncode(dataReader[2].ToString()) + "'>" + dataReader[0].ToString() + "(" + dataReader[1].ToString() + " Byte)</a><br>";
            //    }
            //}

            base.OnInit(e);
        }

#if ENABLE_ARCHIEVE
        #region 邮件归档
        public void MailIncoming(int mailId, int classId)
        {
            if (mailId != 0 && classId != 0)
            {
                try
                {
                    String Username = SiteContext.Current.UserName;
                    MailClass mailclass = new MailClass();
                    DocBody docbody = new DocBody();
                    DocumentClass doc = new DocumentClass();

                    using (var dataReader = mailclass.GetMailCompleteInfoDbreader(mailId.ToString()))
                    {
                        if (dataReader.Read())
                        {
                            var canApprove = AssignRights.IsAccessAllowed(Username, classId, PermissionType.ApproveDocument);

                            docbody.DocTitle = dataReader["MailSubject"].ToString();
                            docbody.DocContent = dataReader["MailBody"].ToString(); ;
                            docbody.DocAddedBy = dataReader["MailSender"].ToString(); ;
                            docbody.DocClassID = classId;
                            docbody.DocAddedDate = DateTime.Now.ToString();
                            docbody.DocApprover = canApprove ? Username : "";
                            docbody.DocApproveDate = canApprove ? DateTime.Now.ToString() : "";
                            docbody.DocApproved = (docbody.DocApprover == "") ? 0 : 1;
                            docbody.DocAttribute = 0;
                            docbody.DocType = 0;

                        }
                        dataReader.Close();
                    }

                    string DocID = doc.AddDocBody(docbody);
                    using (var dataReader = mailclass.GetMailAttInfoDbreader(mailId.ToString()))
                    {
                        while (dataReader.Read())
                        {
                            DocAttachFile docatt = new DocAttachFile();
                            docatt.FileAttribute = 0;
                            docatt.FileSize = Int32.Parse(dataReader["FileSize"].ToString());
                            docatt.FileName = dataReader["FileName"].ToString();
                            docatt.FileAuthor = Username;
                            docatt.FileCatlog = "文档";
                            docatt.FileVisualPath = "Mail" + dataReader["FileVisualPath"].ToString();
                            docatt.FileAddedDate = DateTime.Now.ToString();
                            docatt.DocID = Int32.Parse(DocID);
                            doc.AddAttach(docatt, Int32.Parse(DocID));
                        }
                    }

                    Response.Write("<script language=javascript>alert('归档成功!');</script>");
                }
                catch (Exception oe)
                {
                    Logger.Log(oe.ToString());
                    Server.Transfer("../Error.aspx");
                }
            }
        }
        #endregion
#endif

        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            UDSMail.DeleteMail(
                new int[] { MailID },
                (FolderType != MailFolderType.RecyleBin) ? false : true);
            SiteContext.Current.AlertAndRedirect("邮件删除成功!", "Index.aspx?FolderType=" + (int)FolderType);
        }
    }
}
